﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Common;
namespace ReplayViewer
{
    
    public class LogReader
    {
        List<LogItem> Log_Data = new List<LogItem>();
        List<LogItem> Msg_p = new List<LogItem>();
        
        int index = 0;
        static long Begin_Time=-1;
        public static long Min_Distance = Int64.MaxValue;

        public LogReader(string path)
        {
            StreamReader sr = new StreamReader(path);
            do
            {
                string[] tmp = sr.ReadLine().Split(' ');
                LogItem item = new LogItem(tmp);
                Log_Data.Add(item);
            }
            while (!sr.EndOfStream);



            extractMessageOfPlayer();
            sr.Close();

        }

        private bool Validate(List<LogItem> items,out int pos)
        {
            pos = -1;
            for (int i = 1; i < items.Count; i++)
                if (items[i].Time + Begin_Time < items[i - 1].Time + Begin_Time)
                {
                    pos=i;
                    return false;
                }
            return true;
        }

        public LogItem NextLogItem()
        {
            return Log_Data[index++];
        }
        public LogItem NextTopItem()
        {
            return Log_Data[index];
        }

        public List<int> ReadBoBai(out int p_ID)
        {
            List<int> res = new List<int>();
            LogItem item = NextLogItem();
            if (item.Tag != LogTag.BOBAI)
                throw new Exception("Log File Error at Line " + index);

            p_ID = Int32.Parse(item.Data[0]);

            for(int i=1;i<item.Data.Length;i+=2)
                res.Add(Int32.Parse(item.Data[i]));

            return res;
        }

        public void ReadBaiTrenTay(out List<int> p1, out List<int> p2)
        {
            p1 = new List<int>();
            p2 = new List<int>();

            LogItem item ;
            do
            {
                item = NextLogItem();
                if (item.Tag != LogTag.CHIA_BAI)
                {
                    index--;
                    break;
                }
                if (item.Data[2] == "0")
                    p1.Add(Int32.Parse(item.Data[1]));
                else if (item.Data[2] == "1")
                    p2.Add(Int32.Parse(item.Data[1]));
            }
            while (item.Tag == LogTag.CHIA_BAI);
            
        }
        private void extractMessageOfPlayer()
        {
            Msg_p = new List<LogItem>();
            for (int i = 0; i < this.Log_Data.Count; i++)
                if (!Log_Data[i].is_Tag)
                {
                    Msg_p.Add(Log_Data[i]);
                    Log_Data.RemoveAt(i);
                    i--;
                }
        }

        public List<LogItem> getMessageOfPlayer()
        {
            return Msg_p;
        }

        public class LogItem
        {
            public readonly LogTag Tag;
            public readonly YMSG MSG_Header;
            public readonly bool is_Tag;
            public readonly int User_ID;
            public readonly string[] Data;
            public readonly long Time;
            public LogItem(string[] Data)
            {

                if (Int32.TryParse(Data[0],out User_ID))
                {
                    Tag = LogTag.NONE;
                    is_Tag = false;
                    string tag = Data[2].Substring("RESPONSE_".Length);
                    MSG_Header = (YMSG)Enum.Parse(typeof(YMSG), tag);
                    if (Begin_Time == -1)
                    {
                        Begin_Time = Int64.Parse(Data[1]);
                        this.Time = 0;
                    }
                    else
                    {
                        this.Time = Int64.Parse(Data[1]) - Begin_Time;
                        if (Time < Min_Distance)
                            Min_Distance = Time;
                    }

                        this.Data = new string[Data.Length - 3];

                    for (int i = 3; i < Data.Length; i++)
                    {
                        this.Data[i - 3] = Data[i];
                    }
                }
                else
                {
                    is_Tag = true;
                    this.Tag = (LogTag)Enum.Parse(typeof(LogTag), Data[0]);

                    this.Data = new string[Data.Length - 1];

                    for (int i = 1; i < Data.Length; i++)
                    {
                        this.Data[i - 1] = Data[i];
                    }
                }
            }

            public int getItem(int index)
            {
                return Int32.Parse(Data[index]);
            }

            public object[] getListArg()
            {
                if (is_Tag)
                    return null;

                List<object> res = new List<object>();

                for (int i = 0; i < Data.Length; i++)
                    res.Add(Int32.Parse(Data[i]));

                return res.ToArray();
            }

            public override string ToString()
            {
                return MSG_Header.ToString() + " " + Time.ToString();
            }

        }

    }

    public enum LogTag{
      
        NONE,
        BOBAI,
        SET_PLAYER_ID,
        CHIA_BAI,
        RUT_BAI,
        API_RUTBAI,
        TURN_ID,
        WIN,
        LOSE,
    }
}
